Code for the Parking Lot

Let's write the code for the classes that we have designed, in different languages in this lesson.

We’ve gone over the different aspects of the parking lot system and observed the attributes attached to the problem using various UML diagrams. Let’s explore the more practical side of things, where we will work on implementing the parking lot system using multiple languages. This is usually the last step in an object-oriented design interview process.

We have chosen the following languages to write the skeleton code of the different classes present in the parking lot system:

  • Java

  • C#

  • Python

  • C++

  • JavaScript

Parking lot classes#

In this section, we will provide the skeleton code of the classes designed in the class diagram lesson.

Note: For simplicity, we aren’t defining getter and setter functions. The reader can assume that all class attributes are private and accessed through their respective public getter methods and modified only through their public method functions.

Enumerations and custom data type#

First of all, we will define all the enumerations required in the parking lot. According to the class diagram, there are two enumerations used in the system i.e., PaymentStatus and AccountStatus. The code to implement these enumerations and custom data types is as follows:

Note: JavaScript does not support enumerations, so we will be using the Object.freeze() method as an alternative that freezes an object and prevents further modifications.

Definition for the constants

Parking spots#

The first section of the parking lot system that we will work on is the ParkingSpot class, which will act as a base class for four different types of parking spots: handicapped, compact, large, and motorcycle. This will have an instance of the Vehicle class. The definition of the ParkingSpot class and the classes being derived from it are given below:

ParkingSpot and its derived classes

Vehicle#

Vehicle will be another abstract class, which serves as a parent for four different types of vehicles: car, truck, van, and motor cycle. The definition of the Vehicle and its child classes are given below:

Vehicle and its child classes

Account#

The Account class will be an abstract class, which will have the actors, Admin and ParkingAttendant, as child classes. The definition of these classes is given below:

Account and its child classes

Display board and parking rate#

This section contains the DisplayBoard and ParkingRate classes that only have the composition class with the ParkingLot class. This relationship is highlighted in the ParkingLot class. The definition of these classes is given below:

The DisplayBoard and ParkingRate classes

Entrance and exit#

This section contains the Entrance and Exit classes, both of which are associated with the ParkingTicket class. The definition of the Entrance and Exit classes is given below:

The Entrance and Exit classes

Parking ticket#

The definition of the ParkingTicket class can be found below. This contains instancesof the Vehicle, Payment, Entrance and Exit classes:

The ParkingTicket class

Payment#

The Payment class is another abstract class, with the Cash and CreditCard classes as its child. This takes the PaymentStatus enumeration and the dateTime data type to keep track of the payment status and time. The definition of this class is given below

Payment and its derived classes

Parking lot#

The final class of the parking lot system is the ParkingLot class which will be a Singleton class, meaning the entire system will only have one instance of this class. The definition of this class is given below:

The ParkingLot class

Wrapping up#

We've explored the complete design of a parking lot system in this chapter. We've looked at how a basic parking lot system can be visualized using various UML diagrams and designed using object-oriented principles and design patterns.

Activity Diagram for the Parking Lot

Getting Ready: Elevator System